import {
  Controller,
  Get,
  Post,
  Put,
  Body,
  Query,
  UseGuards,
  Headers,
  Delete,
  Param,
} from '@nestjs/common';
import {
  ApiBearerAuth,
  ApiOperation,
  ApiResponse,
  ApiParam,
  ApiQuery,
  ApiTags,
} from '@nestjs/swagger';
import { AuthGuard } from '@nestjs/passport';

import { BaseResponse } from '@server/basic-dto';

import Dictionaryervice from './service';

import {
  CreateOrUpdateDictionaryTypeDto,
  CreateOrUpdateDictionaryDto,
  QueryDictionariesListResponse,
  QueryDictionariesByTypeResponse,
  AllDictionaryTypeResponse,
} from './dto';

@ApiTags('dictionary')
@Controller('/dictionary')
export default class DictionaryController {
  constructor(private readonly service: Dictionaryervice) {}

  @UseGuards(AuthGuard())
  @ApiOperation({
    summary: '新增一个字典类型',
  })
  @ApiResponse({
    type: BaseResponse,
  })
  @ApiBearerAuth()
  @Put('/create-type')
  async createType(
    @Body() createDictionaryType: CreateOrUpdateDictionaryTypeDto,
    @Headers('authorization') token: string,
  ): Promise<BaseResponse> {
    return this.service.createType(createDictionaryType, token);
  }

  @UseGuards(AuthGuard())
  @ApiOperation({
    summary: '更新一个字典类型',
  })
  @ApiResponse({
    type: BaseResponse,
  })
  @ApiParam({
    name: 'id',
    description: '字典类型id',
  })
  @ApiBearerAuth()
  @Post('/update-type/:id')
  async updateType(
    @Param('id') id: string,
    @Body() updateDictionaryType: CreateOrUpdateDictionaryTypeDto,
  ): Promise<BaseResponse> {
    return this.service.updateType(id, updateDictionaryType);
  }

  @ApiOperation({
    summary: '查询所有字典类型',
  })
  @ApiResponse({
    type: AllDictionaryTypeResponse,
  })
  @ApiBearerAuth()
  @Get('/all-types')
  async allTypes(): Promise<AllDictionaryTypeResponse> {
    return this.service.allTypes();
  }

  @UseGuards(AuthGuard())
  @ApiOperation({
    summary: '新增一个字典',
  })
  @ApiResponse({
    type: BaseResponse,
  })
  @ApiBearerAuth()
  @Put('/create')
  async create(
    @Body() createDictionary: CreateOrUpdateDictionaryDto,
    @Headers('authorization') token: string,
  ): Promise<BaseResponse> {
    return this.service.create(createDictionary, token);
  }

  @UseGuards(AuthGuard())
  @ApiOperation({
    summary: '更新一个字典',
  })
  @ApiResponse({
    type: BaseResponse,
  })
  @ApiParam({
    name: 'id',
    description: '字典id',
  })
  @ApiBearerAuth()
  @Post('/update/:id')
  async update(
    @Param('id') id: string,
    @Body() updateDictionary: CreateOrUpdateDictionaryDto,
  ): Promise<BaseResponse> {
    return this.service.update(id, updateDictionary);
  }

  @UseGuards(AuthGuard())
  @ApiOperation({
    summary: '删除某几个字典, 多个用“,”组合',
  })
  @ApiResponse({
    type: BaseResponse,
  })
  @ApiQuery({
    name: 'ids',
    description: '字典id',
  })
  @ApiBearerAuth()
  @Delete('/delete')
  async delete(@Query('ids') ids: string): Promise<BaseResponse> {
    return this.service.delete(ids);
  }

  @UseGuards(AuthGuard())
  @ApiOperation({
    summary: '字典分页查询',
  })
  @ApiResponse({
    type: QueryDictionariesListResponse,
  })
  @ApiQuery({
    name: 'page',
    description: '当前页',
    required: false,
  })
  @ApiQuery({
    name: 'pageSize',
    description: '每页多少条',
    required: false,
  })
  @ApiBearerAuth()
  @Get('/list')
  async list(
    @Query('page') page: number,
    @Query('pageSize') pageSize: number,
  ): Promise<QueryDictionariesListResponse> {
    return this.service.list(page, pageSize);
  }

  @ApiOperation({
    summary: '按分类查询所有(官网页面)',
  })
  @ApiResponse({
    type: QueryDictionariesByTypeResponse,
  })
  @ApiQuery({
    name: 'typeCode',
    description: '字典类型, 不传默认查询所有类型',
    required: false,
  })
  @ApiQuery({
    name: 'locale',
    description: '语言字典id',
    required: false,
  })
  @Get('/all')
  async all(
    @Query('typeCode') typeCode: string,
    @Query('locale') locale: string,
  ): Promise<QueryDictionariesByTypeResponse> {
    return this.service.all(typeCode, locale);
  }
}
